<?php
session_start();
$pageTitle = "首页";
include 'includes/functions.php';

$articles = getArticlesWithCategories();

// 如果用户已登录，获取用户信息和统计数据
if (isset($_SESSION['user'])) {
    $user = $_SESSION['user'];
    $userArticles = getUserArticles($user['user_id']);
    $stats = getArticleStats($user['user_id']);
}

include 'includes/header.php';
?>

<div class="container">
    <?php if (isset($_SESSION['user'])): ?>
    <div class="welcome-section">
        <h1>欢迎回来，<?php echo htmlspecialchars($user['display_name'] ?? $user['username']); ?>！</h1>
        <p>在这里分享你的想法和故事</p>
    </div>
    
    <div class="stats-section">
        <div class="stat-card">
            <div class="stat-number"><?php echo $stats['total_posts'] ?? 0; ?></div>
            <div class="stat-label">总文章数</div>
        </div>
        <div class="stat-card">
            <div class="stat-number"><?php echo $stats['published_posts'] ?? 0; ?></div>
            <div class="stat-label">已发布</div>
        </div>
        <div class="stat-card">
            <div class="stat-number"><?php echo $stats['total_views'] ?? 0; ?></div>
            <div class="stat-label">总浏览量</div>
        </div>
    </div>
    
    <div class="action-buttons">
        <a href="create.php" class="btn btn-primary">
            <i class="fas fa-plus"></i> 写新文章
        </a>
        <a href="categories.php" class="btn btn-accent">
            <i class="fas fa-tags"></i> 分类管理
        </a>
        <a href="profile.php" class="btn btn-accent">
            <i class="fas fa-user"></i> 个人中心
        </a>
    </div>
    
    <div class="content-section">
        <div class="section-header">
            <h2>我的文章</h2>
            <a href="profile.php" class="view-all">查看全部</a>
        </div>
        
        <?php if (empty($userArticles)): ?>
            <div class="empty-state">
                <i class="fas fa-edit"></i>
                <h3>还没有文章</h3>
                <p>开始创作你的第一篇文章吧！</p>
                <a href="create.php" class="btn">写第一篇文章</a>
            </div>
        <?php else: ?>
            <div class="blog-grid">
                <?php foreach (array_slice($userArticles, 0, 6) as $article): ?>
                    <div class="blog-card">
                        <div class="card-content">
                            <h3><?php echo htmlspecialchars($article['title']); ?></h3>
                            <p class="card-excerpt"><?php echo htmlspecialchars(mb_substr_safe($article['content'], 0, 100)); ?></p>
                            <div class="card-meta">
                                <span><i class="far fa-calendar"></i> <?php echo date('Y-m-d', strtotime($article['created_at'])); ?></span>
                                <span><i class="far fa-eye"></i> <?php echo $article['view_count']; ?></span>
                                <span class="status-badge <?php echo $article['status']; ?>">
                                    <?php echo $article['status'] === 'published' ? '已发布' : '草稿'; ?>
                                </span>
                            </div>
                            <div class="card-actions">
                                <a href="detail.php?id=<?php echo $article['post_id']; ?>" class="btn btn-small">查看</a>
                                <a href="edit.php?id=<?php echo $article['post_id']; ?>" class="btn btn-small btn-accent">编辑</a>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
    <?php else: ?>
    <div class="welcome-section">
        <h1>欢迎来到molin的博客</h1>
        <p>在这里阅读精彩的文章</p>
    </div>
    
    <div class="content-section">
        <div class="section-header">
            <h2>最新文章</h2>
        </div>
        
        <?php if (empty($articles)): ?>
            <div class="empty-state">
                <i class="fas fa-newspaper"></i>
                <h3>暂无文章</h3>
                <p>成为第一个发布文章的人吧！</p>
            </div>
        <?php else: ?>
            <div class="blog-grid">
                <?php foreach (array_slice($articles, 0, 6) as $article): ?>
                    <div class="blog-card">
                        <div class="card-content">
                            <h3><?php echo htmlspecialchars($article['title']); ?></h3>
                            <p class="card-excerpt"><?php echo htmlspecialchars(mb_substr_safe($article['content'], 0, 100)); ?></p>
                            <div class="card-meta">
                                <span><i class="far fa-user"></i> <?php echo htmlspecialchars($article['display_name'] ?? $article['author']); ?></span>
                                <span><i class="far fa-calendar"></i> <?php echo date('Y-m-d', strtotime($article['created_at'])); ?></span>
                                <span><i class="far fa-eye"></i> <?php echo $article['view_count']; ?></span>
                                <?php if (!empty($article['category_names'])): ?>
                                    <span><i class="fas fa-tags"></i> 
                                        <?php 
                                        $categoryId = $article['category_ids'];
                                        $categoryName = $article['category_names'];
                                        if ($categoryId) {
                                            echo '<a href="category.php?id=' . $categoryId . '" class="category-link">' . htmlspecialchars($categoryName) . '</a>';
                                        } else {
                                            echo htmlspecialchars($categoryName);
                                        }
                                        ?>
                                    </span>
                                <?php endif; ?>
                            </div>
                            <div class="card-actions">
                                <a href="detail.php?id=<?php echo $article['post_id']; ?>" class="btn btn-small">阅读全文</a>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
    <?php endif; ?>
</div>

<?php include 'includes/footer.php'; ?> 